package com.quirqy.service;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;


import com.quirqy.model.Person;
import com.quirqy.persistence.QuirqyPersistence;

@Controller
public class RestService 
{

	@RequestMapping( value = "people",method = RequestMethod.GET )
	@ResponseBody
	public final List< Person > getAll()
	{
		QuirqyPersistence qp = new QuirqyPersistence();
		return qp.getAll("select p from Person p ");
	}

	@RequestMapping( value = "people/{id}",method = RequestMethod.GET )
	@ResponseBody
	public final Person get( @PathVariable( "id" ) final Long id )
	{
		QuirqyPersistence qp = new QuirqyPersistence();
		return qp.get(Person.class, id);
	}
	@RequestMapping( value = "people",method = RequestMethod.POST )
	@ResponseStatus( HttpStatus.CREATED )
	@ResponseBody	
	public final Person add( @RequestBody final Person p )
	{
		QuirqyPersistence qp = new QuirqyPersistence();
		return qp.add(p);
	}
	@RequestMapping( value = "people/{id}",method = RequestMethod.PUT )
	@ResponseStatus( HttpStatus.OK )
	public final void set( @PathVariable( "id" ) final Long id, @RequestBody final Person p )
	{
		QuirqyPersistence qp = new QuirqyPersistence();
		qp.set(Person.class, id, p);
	}


/*	@RequestMapping( value = "people",method = RequestMethod.GET )
	@ResponseBody
	public final List< Person > getAll()
	{
		EntityManager em = EMF.get().createEntityManager();
		return em.createQuery("select p from Person p ").getResultList();
	}

	@RequestMapping( value = "people/{id}",method = RequestMethod.GET )
	@ResponseBody
	public final Person get( @PathVariable( "id" ) final Long id )
	{
		EntityManager em = EMF.get().createEntityManager();
		return em.find(Person.class, id);
	}
	*/
}
